#include "Light.h"

int Omni::read(TiXmlElement* light)
{
	type=1;
	string test=light->Attribute("enabled");
	if (test.compare("1")==0)
		enabled=true;
	else enabled=false;

	TiXmlElement* alocation=light->FirstChildElement();
	TiXmlElement* aambient=alocation->NextSiblingElement();
	TiXmlElement* adiffuse=aambient->NextSiblingElement();
	TiXmlElement* aspecular=adiffuse->NextSiblingElement();

	if(alocation->QueryFloatAttribute("x",&location[0])==TIXML_SUCCESS&&
	    alocation->QueryFloatAttribute("y",&location[1])==TIXML_SUCCESS&&
        alocation->QueryFloatAttribute("z",&location[2])==TIXML_SUCCESS&&
	    alocation->QueryFloatAttribute("w",&location[3])==TIXML_SUCCESS&&

		aambient->QueryFloatAttribute("r",&ambient[0])==TIXML_SUCCESS&&
		aambient->QueryFloatAttribute("g",&ambient[1])==TIXML_SUCCESS&&
		aambient->QueryFloatAttribute("b",&ambient[2])==TIXML_SUCCESS&&
		aambient->QueryFloatAttribute("a",&ambient[3])==TIXML_SUCCESS&&

		adiffuse->QueryFloatAttribute("r",&diffuse[0])==TIXML_SUCCESS&&
		adiffuse->QueryFloatAttribute("g",&diffuse[1])==TIXML_SUCCESS&&
		adiffuse->QueryFloatAttribute("b",&diffuse[2])==TIXML_SUCCESS&&
		adiffuse->QueryFloatAttribute("a",&diffuse[3])==TIXML_SUCCESS&&

		aspecular->QueryFloatAttribute("r",&specular[0])==TIXML_SUCCESS&&
		aspecular->QueryFloatAttribute("g",&specular[1])==TIXML_SUCCESS&&
		aspecular->QueryFloatAttribute("b",&specular[2])==TIXML_SUCCESS&&
		aspecular->QueryFloatAttribute("a",&specular[3])==TIXML_SUCCESS)
	 {}
	 else{////cout<<"Erro na leitura de luz omni."<<endl;
	 return -1;}

	// //cout<<"luz:"<<type<<"  "<<enabled<<"  "<<location[0]<<"  "<<location[1]<<"  "<<location[2]<<"  "<<location[3]<<endl;

	 return 0;



}

int Spot::read(TiXmlElement* light)
{
	type=2;
	string test=light->Attribute("enabled");
	
	if (test.compare("1")==0)
		enabled=true;
	else enabled=false;

	if(light->QueryFloatAttribute("angle",&angle)==TIXML_SUCCESS&&
	   light->QueryFloatAttribute("exponent",&exponent)==TIXML_SUCCESS)
	{}
	else{////cout<<"Erro na leitura de parametros spot."<<endl;
	return -1;}

	 TiXmlElement* alocation=light->FirstChildElement();
	 TiXmlElement* atarget=alocation->NextSiblingElement();
	 TiXmlElement* aambient=atarget->NextSiblingElement();
	 TiXmlElement* adiffuse=aambient->NextSiblingElement();
	 TiXmlElement* aspecular=adiffuse->NextSiblingElement();

	 if(alocation->QueryFloatAttribute("x",&location[0])==TIXML_SUCCESS&&
	    alocation->QueryFloatAttribute("y",&location[1])==TIXML_SUCCESS&&
        alocation->QueryFloatAttribute("z",&location[2])==TIXML_SUCCESS&&
	    alocation->QueryFloatAttribute("w",&location[3])==TIXML_SUCCESS&&

	    atarget->QueryFloatAttribute("x",&target.x)==TIXML_SUCCESS&&
	    atarget->QueryFloatAttribute("y",&target.y)==TIXML_SUCCESS&&
	    atarget->QueryFloatAttribute("z",&target.z)==TIXML_SUCCESS&&
	    
		aambient->QueryFloatAttribute("r",&ambient[0])==TIXML_SUCCESS&&
		aambient->QueryFloatAttribute("g",&ambient[1])==TIXML_SUCCESS&&
		aambient->QueryFloatAttribute("b",&ambient[2])==TIXML_SUCCESS&&
		aambient->QueryFloatAttribute("a",&ambient[3])==TIXML_SUCCESS&&

		adiffuse->QueryFloatAttribute("r",&diffuse[0])==TIXML_SUCCESS&&
		adiffuse->QueryFloatAttribute("g",&diffuse[1])==TIXML_SUCCESS&&
		adiffuse->QueryFloatAttribute("b",&diffuse[2])==TIXML_SUCCESS&&
		adiffuse->QueryFloatAttribute("a",&diffuse[3])==TIXML_SUCCESS&&

		aspecular->QueryFloatAttribute("r",&specular[0])==TIXML_SUCCESS&&
		aspecular->QueryFloatAttribute("g",&specular[1])==TIXML_SUCCESS&&
		aspecular->QueryFloatAttribute("b",&specular[2])==TIXML_SUCCESS&&
		aspecular->QueryFloatAttribute("a",&specular[3])==TIXML_SUCCESS)
	 {}
	 else{////cout<<"Erro na leitura de luz spot."<<endl;
	 return -1;}

	 return 0;
}